import {
	mapState
} from "vuex";
import {
	area
} from '@/common/api.js'
import {
	baseUrl
} from '@/common/config.js'

export default {
	data() {
		return {
			baseUrl,
			pickerLoading:false
		}
	},
	computed: {
		...mapState(["$userInfo", "isLogin", "systemInfo", "imgData", "areaData", "configData", "token", "cityInfo", "city", "cityName"]),
		columns() {
			console.log('this.areaData', this.areaData);
			return this.areaData;
		}
	},
	onShow() {},
	methods: {
		jumpPage(url) {
			console.log(this.isLogin);
			if (this.isLogin) {
				uni.navigateTo({
					url
				})
			} else {
				uni.reLaunch({
					url: '/pages/common/login'
				})
				return false;
				this.$refs.uToast.show({
					type: 'error',
					message: '未登录',
					complete() {
						uni.reLaunch({
							url: '/pages/common/login'
						})
					}
				})
				// this.$refs.login.show = true;
			}
		},
	
		paydata(res) {
			uni.showLoading({
				title: '支付中...'
			})
			return new Promise((resolve, reject) => {
				uni.requestPayment({
					provider: 'wxpay', // 服务提提供商
					timeStamp: res.timeStamp, // 时间戳
					nonceStr: res.nonceStr, // 随机字符串
					package: res.package,
					signType: res.signType, // 签名算法
					paySign: res.paySign, // 签名
					success: function(res) {
						console.log('支付成功', res);
						uni.hideLoading();
						resolve(1);
					},
					fail: function(err) {
						uni.hideLoading();
						console.log('支付失败', err);
						reject(0)
					}
				})
			})

		},
		changeHandler(e) {
			const {
				columnIndex,
				value,
				values, // values为当前变化列的数组内容
				index,
				// 微信小程序无法将picker实例传出来，只能通过ref操作
				picker = this.$refs.uPicker
			} = e
			// 当第一列值发生变化时，变化第二列(后一列)对应的选项
			if (columnIndex === 0) {
				let data = []
				this.pickerLoading = true
				// picker为选择器this实例，变化第二列对应的选项
				this.getArea(value[0].value).then(res => {
					picker.setColumnValues(1, res)
					let data = res;
					this.getArea(value[0].value, data[0].value).then(res => {
						picker.setColumnValues(2, res)
						this.pickerLoading = false
					})
				})
			}
			if (columnIndex === 1) {
				this.pickerLoading = true
				this.getArea(value[0].value, value[1].value).then(res => {
					picker.setColumnValues(2, res)
					this.pickerLoading = false
				})
			}
		},
		getArea(province, city) {
			return new Promise((resolve, reject) => {
				let params = {};
				if (province) params.province = province;
				if (city) params.city = city;
				area({
					data: params
				}).then(res => {
					resolve(res)
				})
			})
		},
		
		// 删除图片
		deletePic(event) {
			this[`fileList${event.name}`].splice(event.index, 1)
		},
		// 新增图片
		async afterRead(event) {
			// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
			let lists = [].concat(event.file)
			let fileListLen = this[`fileList${event.name}`].length
			lists.map((item) => {
				this[`fileList${event.name}`].push({
					...item,
					status: 'uploading',
					message: '上传中'
				})
			})
			for (let i = 0; i < lists.length; i++) {
				const result = await this.uploadFilePromise(lists[i].url)
				let item = this[`fileList${event.name}`][fileListLen]
				this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
					status: 'success',
					message: '',
					url: result
				}))
				fileListLen++
			}
		},
		uploadFilePromise(url) {
			return new Promise((resolve, reject) => {
				let a = uni.uploadFile({
					url: baseUrl + '/api/common/upload', // 仅为示例，非真实的接口地址
					filePath: url,
					name: 'file',
					formData: {
						token: this.token
					},
					success: (res) => {
						let data = JSON.parse(res.data);
						setTimeout(() => {
							console.log('res', data.data.url);
							resolve(data.data.url)
						}, 1000)
					}
				});
			})
		},
	}
}